<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 元素值大于变化阈值的子数组 -->
    <script>
      var validSubarraySize = function (nums, threshold) {
        let n = nums.length
        let left = new Array(n).fill(-1)
        let right = new Array(n).fill(n)
        let stack = []
        for (let i = 0; i < n; i++) {
          while (stack.length && nums[last(stack)] >= nums[i]) stack.pop()
          left[i] = stack.length ? last(stack) : -1
          stack.push(i)
        }
        stack.length = 0
        for (let i = n - 1; i >= 0; i--) {
          while (stack.length && nums[last(stack)] >= nums[i]) stack.pop()
          right[i] = stack.length ? last(stack) : n
          stack.push(i)
        }
        for (let i = 0; i < n; i++) {
          let k = right[i] - left[i] - 1
          if (nums[i] > threshold / k) {
            return k
          }
        }
        return -1
      }
      function last(stack) {
        return stack[stack.length - 1]
      }
      console.log(validSubarraySize([1, 3, 4, 3, 1], 6))
    </script>
  </body>
</html>
